package com.vinjogames.gfx;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

import com.vinjogames.som.R;
import com.vinjogames.util.Logger;

public class SpriteSheet {	
	private static int MAX_TEXTURES = 50;
	private Bitmap mSpriteSheet;
	private Bitmap mUISheet;
	private Bitmap mBackgroundSheet;
	private Bitmap mWaterSheet;
	private BitmapFactory.Options options; 
	
	private Bitmap mFontBitmap;
	private Context mContext;
	
	private int[][] allTextures = new int[MAX_TEXTURES][];
	
	private SpriteSheet(){};

	private static class SpriteSheetHolder {
		public static final SpriteSheet instance = new SpriteSheet();
	}

	public static SpriteSheet getInstance(){
		return SpriteSheetHolder.instance;
	}
	
	public void setContext(Context context){ mContext = context; }
	
	public int[] getTexture(int key){
		return allTextures[key];
	}
	
	public void loadBitmaps(){
//		 options.inSampleSize =2;
		
		if(mSpriteSheet == null){
			mSpriteSheet = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.boat , options);
		}
		if(mWaterSheet == null){
			mWaterSheet = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.watergrass,options);
		}
		
		if(mFontBitmap == null){
			mFontBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.font,options);
		}
	}
	
	public void loadAllTextures(GL10 gl){
//		loadBackgroundTextures(gl);
		
		loadBitmaps();
		
		
		allTextures[TextureId.DOWN_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mSpriteSheet, 0, 0, 256, 64), 64, 64);
		allTextures[TextureId.LEFT_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mSpriteSheet, 0, 64, 256, 64), 64, 64);
		allTextures[TextureId.RIGHT_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mSpriteSheet, 0, 128, 256, 64), 64, 64);
		allTextures[TextureId.UP_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mSpriteSheet, 0, 192, 256, 64), 64, 64);

		allTextures[TextureId.GRASS_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mWaterSheet, 0,0, 32, 32), 32, 32);
		
		allTextures[TextureId.WATER_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mWaterSheet, 0, 160, 96, 32), 32, 32);//96
		allTextures[TextureId.DIRT1_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mWaterSheet, 0, 64, 32, 32), 32, 32);
		allTextures[TextureId.DIRT2_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mWaterSheet, 32, 64, 32, 32), 32, 32);
		allTextures[TextureId.DIRT3_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mWaterSheet, 64, 64, 32, 32), 32, 32);
		allTextures[TextureId.DIRT4_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mWaterSheet, 0, 96, 32, 32), 32, 32);
		allTextures[TextureId.DIRT5_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mWaterSheet, 32, 96, 32, 32), 32, 32);
		allTextures[TextureId.DIRT6_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mWaterSheet, 64, 96, 32, 32), 32, 32);
		allTextures[TextureId.DIRT7_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mWaterSheet, 0, 128, 32, 32), 32, 32);
		allTextures[TextureId.DIRT8_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mWaterSheet, 32,128, 32, 32), 32, 32);
		allTextures[TextureId.DIRT9_TEXTURE_ID] = loadTexture(gl, Bitmap.createBitmap(mWaterSheet, 64,128, 32, 32), 32, 32);

		mSpriteSheet.recycle();
		mSpriteSheet = null;
		
		mWaterSheet.recycle();
		mWaterSheet = null;

	}
	
	public void loadUITextures(GL10 gl){
		options = new BitmapFactory.Options();
		options.inPurgeable = true;
		
    	long time = System.nanoTime();
//		if(mUISheet == null){
//			mUISheet = BitmapFactory.decodeResource(mContext.getResources(), 0, options);
//		}
		
		if(mFontBitmap == null){
			mFontBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.font,options);
		}
		
		int textureID = 0;
		
		//allTextures[textureID]   	= loadTexture(gl, Bitmap.createBitmap(mUISheet, 1024, 0, 1024, 128), 128, 128);
		
		
		//mUISheet.recycle();
		//mUISheet = null;
		
		long elapse = System.nanoTime() - time;
		Logger.log("loading ui took: " + elapse/1000000);

//		new Thread(new Runnable() {
//			public void run() {
//				int backgroundImageID = 0; //TODO:
//				LoadingScreen.getInstance().init(backgroundImageID);
//			}
//		}).start();
	}

	public void loadBackgroundTextures(GL10 gl){
		int backgroundImageBitmapID = 0;//TODO;
		if(mBackgroundSheet == null){
			mBackgroundSheet = BitmapFactory.decodeResource(mContext.getResources(), backgroundImageBitmapID, options);
		}
		int textureID = 0;
		//allTextures[textureID] = loadTexture(gl, Bitmap.createBitmap(mBackgroundSheet, 0, 1536, 2048, 512), 512, 512);
		
		//mBackgroundSheet.recycle();
		//mBackgroundSheet = null;
	}
	
	private int[] loadTexture(GL10 gl, Bitmap bitmap, int frameWidth, int frameHeight){
		int frames = bitmap.getWidth() / frameWidth;
		int[] textures = new int[frames];
		// Generate Texture Pointer
		gl.glGenTextures(frames, textures, 0);
		for(int i = 0; i < frames; i++){
			Bitmap currentFrame = frames == 1 ? bitmap : Bitmap.createBitmap(bitmap, i*frameWidth, 0, frameWidth, frameHeight);
			// Bind to Array
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);
			// Create Filtered Texture													NEAREST = Crisp  LINEAR = Blurred
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
			// Specify Image for Bitmap
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, currentFrame, 0);
			currentFrame.recycle();
		}
		return textures;
	}
	
	public Bitmap getFontBitmap(){
		return mFontBitmap;
	}
}
